package com.shop.Control;

import java.util.Map;

import javax.websocket.Session;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;
import com.shop.actionentity.MerchantLogin;
import com.shop.service.MerchantServiceImpl;
import com.shop.util.FormAjaxUtil;

/**
 * 
 * <b>商户登录<b>
 * @author 威 
 * <br>2017年12月
 *
 */
public class MerchantAction extends SuperActionSupport implements ModelDriven<MerchantLogin>{
	private static final long serialVersionUID = 1L;
	
	private MerchantLogin mLogin ;
	@Override
	public MerchantLogin getModel() {
		return mLogin;
	}
	/*
	 * 获取前台修改商品的参数
	 */
	private String data ;
	public String getData() {
		return data;
	}
	public void setData(String data) {
		this.data = data;
	}
	
	@SuppressWarnings({"rawtypes" })
	public String login() {
		MerchantServiceImpl merchantServiceImpl = (MerchantServiceImpl) getBean("merchantServiceImpl") ;
		System.out.println("login...start") ;
		System.out.println(mLogin) ;
		ActionContext context=ActionContext.getContext() ;
		Map parameterMap=context.getParameters();
		String username = ((String[]) parameterMap.get("user"))[0] ;
		String password = ((String[]) parameterMap.get("password"))[0] ;
		String callbackObj = ((String[]) parameterMap.get("callbackObj"))[0] ;
		System.out.println(password) ;
		try{
			Object[] result = merchantServiceImpl.login(username, password) ;
			switch((String) result[1]){
				case "0000":  
					System.out.println("当前登录者id为："+result[0]);
					context.getSession().put("merchant_id", result[0]) ;
					System.out.println("测试数据"+context.get("merchant_id"));
					print(FormAjaxUtil.successAccess(callbackObj, "true")) ;
					break;
				case "0001":  
					print(FormAjaxUtil.failAccess(callbackObj, "0001")) ;
					break;
				case "0002":  
					print(FormAjaxUtil.failAccess(callbackObj, "0002")) ;
					break;
			}
		}catch (RuntimeException e){
			System.out.println("异常失败") ;
			print(FormAjaxUtil.failAccess(callbackObj, "服务器异常")) ;
		}
		return SUCCESS ;
	}
	public String linkTo(){
		try{
			ActionContext context=ActionContext.getContext() ;
			System.out.println(data) ;
			context.getSession().put("linkType", data) ;
			print("true") ;
			System.out.println("成功跳转") ;
		}catch(RuntimeException e){
			System.out.println("异常") ;
			print("false") ;
		}
		return SUCCESS ;
	}
}

